package com.lishao.soccer.action;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.providers.encoding.Md5PasswordEncoder;

import com.lishao.soccer.bean.entity.SmUser;
import com.lishao.soccer.service.SmUserService;
import com.lishao.system.DateJsonValueProcessor;
import com.lishao.system.security.providers.SecurityUser;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class ChangePasswordAction extends ActionSupport {
	SmUserService smUserService;
	
	private String oldPassword;
	private String newPassword1;
	private String newPassword2;
	
	public String savePassword() throws Exception{
		String strMessage = "";
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();

		HttpSession session = request.getSession();
		SecurityUser secUser = (SecurityUser)session.getAttribute("LOGIN_KEY");
		String oldPasswordMd5 = new Md5PasswordEncoder().encodePassword(oldPassword, null);
		if(secUser.getPassword().equals(oldPasswordMd5)){
			String newPasswordMd5 = new Md5PasswordEncoder().encodePassword(newPassword1, null);
			
			SmUser smUser = secUser.getSmUser();
			smUser.setUserPassword(newPasswordMd5);
			smUserService.updateByPrimaryKey(smUser);
			secUser.setPassword(newPasswordMd5);
			strMessage = "修改密码成功！";
		}else{
			strMessage = "旧密码输入错误！";
		}
		Map resMap = new HashMap();
		resMap.put("strMessage", strMessage);
		writeMap(resMap);
		return null;
	}
	
	protected final void writeMap(Map result) throws Exception{
		JsonConfig cfg = new JsonConfig();  
		cfg.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor()); 
		cfg.registerJsonValueProcessor(Timestamp.class, new DateJsonValueProcessor()); 
		JSONObject jsonObject = JSONObject.fromObject(result,cfg);
		System.out.println(jsonObject.toString());
		HttpServletResponse response = ServletActionContext.getResponse();
		response.getWriter().write(jsonObject.toString());
	}
	//������get set����
	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword1() {
		return newPassword1;
	}

	public void setNewPassword1(String newPassword1) {
		this.newPassword1 = newPassword1;
	}

	public String getNewPassword2() {
		return newPassword2;
	}

	public void setNewPassword2(String newPassword2) {
		this.newPassword2 = newPassword2;
	}

	public SmUserService getSmUserService() {
		return smUserService;
	}

	public void setSmUserService(SmUserService smUserService) {
		this.smUserService = smUserService;
	}
	
}
